package com.zhg.javakc.modules.masterialstype.controller;

import com.zhg.javakc.base.util.CommonUtil;
import com.zhg.javakc.modules.masterialstype.entity.TypeEntity;
import com.zhg.javakc.modules.masterialstype.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("type")
public class TypeController {
    @Autowired
    private TypeService typeService;

    @RequestMapping("queryAlltype")
    @ResponseBody
    public List<Map<String,Object>> queryAlltype(){
        List<Map<String,Object>> list=typeService.queryAlltype();
        return list;
    }
    @RequestMapping("create")
    public String  save(TypeEntity typeEntity){
        typeEntity.setGamTypeId(CommonUtil.uuid());
        typeService.save(typeEntity);
        return "masterialstype/list";
    }

    @RequestMapping("queryById")
    public String queryById(String gamTypeId, ModelMap map){
       TypeEntity entity= typeService.get(gamTypeId);
       map.put("entity",entity);
        return "masterialstype/update";
    }

    @RequestMapping("update")
    public  String update(TypeEntity typeEntity){
        typeService.update(typeEntity);
        return "masterialstype/list";
    }
    @RequestMapping("delete")
    public String delete(String gamTypeId){
       List<Map<String,Object>> list= typeService.queryById(gamTypeId);
       if(list!=null&&list.size()>0){
           for(Map<String,Object> map:list){
              delete(map.get("id").toString());
           }
           typeService.deleteType(gamTypeId);
       }else {
           typeService.deleteType(gamTypeId);
       }
        return "masterialstype/list";
    }
}
